The first two cells below are used to tell the notebook that we want any plots (generated by matplotlib) to appear inline with the notebook and not in a separate window.


In [68]:
%matplotlib inline

In [69]:
import matplotlib.pyplot as plt

Next, we import all of the functions that come with the numpy package. This gives python a lot of array-based features that are optimized for computational speed.


In [70]:
from numpy import *

The first is the command linspace

linspace creates an array filled with values set by the function parameters:


In [71]:
x = linspace(0,100, num=100)

In [72]:
x


Out[72]:
array([   0.        ,    1.01010101,    2.02020202,    3.03030303,
          4.04040404,    5.05050505,    6.06060606,    7.07070707,
          8.08080808,    9.09090909,   10.1010101 ,   11.11111111,
         12.12121212,   13.13131313,   14.14141414,   15.15151515,
         16.16161616,   17.17171717,   18.18181818,   19.19191919,
         20.2020202 ,   21.21212121,   22.22222222,   23.23232323,
         24.24242424,   25.25252525,   26.26262626,   27.27272727,
         28.28282828,   29.29292929,   30.3030303 ,   31.31313131,
         32.32323232,   33.33333333,   34.34343434,   35.35353535,
         36.36363636,   37.37373737,   38.38383838,   39.39393939,
         40.4040404 ,   41.41414141,   42.42424242,   43.43434343,
         44.44444444,   45.45454545,   46.46464646,   47.47474747,
         48.48484848,   49.49494949,   50.50505051,   51.51515152,
         52.52525253,   53.53535354,   54.54545455,   55.55555556,
         56.56565657,   57.57575758,   58.58585859,   59.5959596 ,
         60.60606061,   61.61616162,   62.62626263,   63.63636364,
         64.64646465,   65.65656566,   66.66666667,   67.67676768,
         68.68686869,   69.6969697 ,   70.70707071,   71.71717172,
         72.72727273,   73.73737374,   74.74747475,   75.75757576,
         76.76767677,   77.77777778,   78.78787879,   79.7979798 ,
         80.80808081,   81.81818182,   82.82828283,   83.83838384,
         84.84848485,   85.85858586,   86.86868687,   87.87878788,
         88.88888889,   89.8989899 ,   90.90909091,   91.91919192,
         92.92929293,   93.93939394,   94.94949495,   95.95959596,
         96.96969697,   97.97979798,   98.98989899,  100.        ])

You can also find out more about a function by asking for help on the command line. Type the function name and a question mark:


In [73]:
linspace?

You should see a line that says:

Signature: linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)

This describes the function and what values you can specify. We used only the start and stop but we could also use the num parameter to tell it how many points we want in the array.

You can close the help by typing esc or by clicking on the small x in the upper right corner.


In [86]:
x = linspace(0,100,num=1024)
x


Out[86]:
array([  0.00000000e+00,   9.77517107e-02,   1.95503421e-01, ...,
         9.98044966e+01,   9.99022483e+01,   1.00000000e+02])

Starting from the example below, generate a few plots for practice:

  • a sine wave
  • the fourier transform of a sine wave
  • a sine wave with additional noise (add random values that are 10% of the sine-wave amplitude)
  • the fourier transform of the noisy sine wave

Hints: for these, you'll want to have a larger number of points in the array and we don't necessarily need to view the array each time, that was just for learning purposes. Try setting num=1024 in the linspace function.

The following functions will be useful (these are both part of numpy):

  • random.random()
  • fft.fft()

The dot indicates that the function is part of a module within numpy; the fft function is part of the fft module so you have to specify that when you use it. You can learn more about them with the help feature:

random.random?


In [87]:
y = sin(x)

In [88]:
plt.plot(x,y)


Out[88]:
[<matplotlib.lines.Line2D at 0x7fe22ddfe5d0>]

In [89]:
plt.plot(x,y,".")  #another example, with dots this time...


Out[89]:
[<matplotlib.lines.Line2D at 0x7fe22dd18d10>]

In [90]:
random.random()


Out[90]:
0.7181697565986422

In [117]:
fft.fft(y)
newy=fft.fft(y)

In [119]:
plt.plot(abs(newy[0:100]))


Out[119]:
[<matplotlib.lines.Line2D at 0x7fe22cedf590>]

In [100]:
newarry = y + .1*random.random(1024)

In [101]:
newarry


Out[101]:
array([ 0.05709852,  0.12897035,  0.25012105, ..., -0.59526375,
       -0.52251789, -0.42510798])

In [ ]:


In [102]:
plt.plot(x,newarry)


Out[102]:
[<matplotlib.lines.Line2D at 0x7fe22d7373d0>]

In [103]:
fft.fft(newarry)


Out[103]:
array([ 52.40925171+0.j        ,   1.72887977+0.74388513j,
         1.35086460+0.85887292j, ...,   2.42503762-1.77490685j,
         1.35086460-0.85887292j,   1.72887977-0.74388513j])

In [121]:
plt.plot(abs(fft.fft(newarry))[0:40])


Out[121]:
[<matplotlib.lines.Line2D at 0x7fe22cd481d0>]

In [ ]:


In [ ]:


In [ ]:


In [ ]:


In [ ]: